package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Food;
import com.woniuxy.oldmanapi.mapper.FoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodService {
    @Autowired
    FoodMapper foodMapper;

    public PageInfo<Food> initSelectAllByCons(String name, int now, int size) {
        PageHelper.startPage(now, size);
        List<Food> foods = foodMapper.initSelectAllByCons(name);
        return new PageInfo<>(foods);
    }

    public void addFood(Food food) {
        food.setFoodstatus(1);
        if (food.getFoodimg() == null) {
            food.setFoodimg("R-C.jpg");
        }
        foodMapper.insert(food);
    }

    public void modifyFood(Food food) {
        if (food.getFoodimg() == null) {
            food.setFoodimg("R-C.jpg");
        }
        foodMapper.updateByPrimaryKey(food);
    }

    public void modifyFoodStatus(Integer id, Integer status) {
        foodMapper.modifyFoodStatus(id, status);
    }

    public void deleteFoodById(Integer id) {
        foodMapper.deleteByPrimaryKey(id);
    }

    public Food selectFoodById(Integer id) {
        Food food = foodMapper.selectByPrimaryKey(id);
        return food;
    }

    //删除的中间表
    public void deleteFoodByRecipeId(Integer foodId, Integer recipeId) {
        foodMapper.deleteFoodByRecipeId(foodId, recipeId);
    }

    public PageInfo<Food> selectAll(String name, int now, int size) {
        PageHelper.startPage(now, size);
        List<Food> foods = foodMapper.selectAllByStatus(name);
        return new PageInfo<>(foods);
    }

    public void addFoodByRecipeId(Integer foodId, Integer recipeId) {
        foodMapper.addFoodByRecipeId(foodId,recipeId);
    }

    public void deleteAllFoodByRecipeId(Integer recipeId) {
        foodMapper.deleteAllFoodByRecipeId(recipeId);
    }
}
